package com.atcx.exception;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Copyright (C), 2017-2022, MIDOU
 * ClassName: ExceptionHandler
 * Author:   pangshu
 * Date:     2022/7/21 17:27
 * Version: 1.0
 * Description:
 */
@Component
public class ExceptionHandler implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        ModelAndView view = new ModelAndView();
        view.setViewName("exception");
        if (ex instanceof NullPointerException){
            view.addObject("msg","空指针异常1461651516");

        }
        if (ex instanceof ArithmeticException){
            view.addObject("msg","算术异常");
        }
        if (ex instanceof AtcxException) {
            String message = ex.getMessage();
            String errorCode = ((AtcxException) ex).getErrorCode();
            view.addObject("msg","错误编码："+errorCode+","+"提示信息："+message);
        }
        return view;
    }
}
